﻿using System;
using System.Collections.Generic;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;
using System.Web.UI;
using shfa.snomtastic.data;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eGroups : EditBase
    {

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
             BindGridItems();
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);
            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "ClientEvents", common.DblClickScript, true);
        }

        #endregion

        #region Event Handlers
         protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                e.Canceled = true;
                Guid id = new Guid(rgListItems.Items[e.Item.ItemIndex].GetDataKeyValue("GroupId").ToString());
                RaiseNavEvent(this, new NavEventArgs("eGroup", id));
            }
        }

        #endregion

        public void BindGridItems()
        {
            GroupController gc = new GroupController();
            List<GroupInfo> giList = gc.SelectGroups("Name Asc");

            ConfigurationController cc = new ConfigurationController();


            foreach (GroupInfo gi in giList)
            {
                if (gi.ConfigurationId != Guid.Empty)
                {
                    ConfigurationInfo ci = cc.SelectConfiguration(gi.ConfigurationId);
                    if (ci!=null)
                    {
                        gi.Configuration = ci.Name;
                    }
                }
            }
            
            rgListItems.DataSource = giList;
            rgListItems.DataBind();


        }


        #region Action Events
        public void DeleteItem(Guid id)
        {
            try
            {
                GroupController gc = new GroupController();
                gc.DeleteGroup(id);
            }
            catch {}

            RaiseNavEvent(this, new NavEventArgs("eGroups", id));
        }


        #endregion


        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Add":
                    RaiseNavEvent(this, new NavEventArgs("eGroup", new Guid()));                        
                    break;
                case "Edit":
                    if (rgListItems.SelectedValue != null)
                    {
                        RaiseNavEvent(this, new NavEventArgs("eGroup", new Guid(rgListItems.SelectedValue.ToString())));
                    }
                    break;
                case "Delete":
                    if (rgListItems.SelectedValue != null)
                    {
                        DeleteItem(new Guid(rgListItems.SelectedValue.ToString()));
                    }
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get 
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Add", "Add Group", "~/images/icons/add.png"));
                al.Add(new ActionInfo("Edit", "Edit Group", "~/images/icons/edit.png"));
                al.Add(new ActionInfo("Delete", "Delete Group", "~/images/icons/delete.png", "Are you sure you want to delete this Group?", true));
                return al;
            }
        }

        #endregion

    }
}